package com.hanggold;

import com.hanggold.initializer.DemoServletContainerInitializer;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;

import java.io.File;
import java.io.IOException;
import java.util.Collections;

/**
 * @author <a href="mailto:sunmch@163.com">smc</a>
 * @date 2019-07-19 08:19
 * @since
 **/
public class TwoMainTomatDemo {

    public static void main(String[] args) throws LifecycleException, IOException {


        Tomcat tomcat = new Tomcat();

        Connector connector = new Connector();

        connector.setPort(8080);

        File baseDir = File.createTempFile("tomcat-dir-9900.",".smc");

        baseDir.delete();

        baseDir.mkdir();

        tomcat.setBaseDir(baseDir.getAbsolutePath());

        tomcat.setConnector(connector);

        tomcat.getHost().setAutoDeploy(false);

        StandardContext standardContext = new StandardContext();

        File docBase = File.createTempFile("tomcat-docbase-9900.",".smc");

        docBase.delete();

        docBase.mkdir();

        standardContext.setDocBase(docBase.getAbsolutePath());

        standardContext.setName("");

        standardContext.setPath("");

        standardContext.setSessionTimeout(10);

//        Collections.synchronizedSet()

        standardContext.addServletContainerInitializer(new DemoServletContainerInitializer(), Collections.EMPTY_SET);



//        Thread.currentThread().getContextClassLoader().getResources("")



        tomcat.getHost().addChild(standardContext);


        tomcat.start();

        System.out.println("嵌入式tomcat开启");

        tomcat.getServer().await();

    }

}
